  /**
 *
 *               Copyright (c) 2010 by Anole
 *
 * All rights reserved.  These materials are confidential and
 * proprietary to NCS. and no part of these materials
 * should be reproduced, published in any form by any means,
 * electronic or mechanical, including photocopy or any
 * Information storage or retrieval system nor should the
 * materials be disclosed to third parties without the express
 * written authorization of BALANCE.
 *
 *<Class Description>
 *
 *
 *<Module Name>
 *
 *
 *<Change Revision>
 *====================================================
 *Date                 Author                Remarks
 *
*/

package com.balance.anole.client.common.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.balance.anole.business.to.ProductTO;
import com.balance.anole.business.vo.ProductSearchTO;
import com.balance.anole.client.common.dao.ProductDAO;

public class ProductDAOImpl extends BaseDAOImpl implements ProductDAO {


	@SuppressWarnings("unchecked")
	@Override
	public List<ProductTO> getProducts(final ProductSearchTO searchTO) {
		List<ProductTO> executeFind = getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				String hql = "from com.balance.anole.business.to.ProductTO p where ";

				if(searchTO.getProductId()!=null && !"".equals(searchTO.getProductId())){
					hql += " p.pdtId = :productId and";
				}

				if(searchTO.getProductDesc()!=null && !"".equals(searchTO.getProductDesc())){
					hql += " p.pdtDesc = :productDesc and";
				}

				if(searchTO.getProductTypeTO()!=null && !"".equals(searchTO.getProductTypeTO())){
					hql += " p.pdtType = :productTypeTO and";
				}

				if (searchTO.getSeasonTO()!=null && !"".equals(searchTO.getSeasonTO())){
					hql += " p.season = :seasonTO and";
				}

				if (searchTO.getSexTO()!=null && !"".equals(searchTO.getSexTO())){
					hql += " p.sex = :sexTO and";
				}

				if (searchTO.getAssistCode()!=null && !"".equals(searchTO.getAssistCode())){
					hql += " p.assistCode = :assistCode";
				}

				hql = hql.trim();

				if (hql.endsWith("and")) {
					hql = hql.substring(0, hql.length() - 3);
				}

				Query q = getSession().createQuery(hql);

				if(searchTO.getProductId()!=null && !"".equals(searchTO.getProductId())){
					q.setParameter("productId", searchTO.getProductId());
				}

				if(searchTO.getProductDesc()!=null && !"".equals(searchTO.getProductDesc())){
					q.setParameter("productDesc", searchTO.getProductDesc());
				}

				if(searchTO.getProductTypeTO()!=null && !"".equals(searchTO.getProductTypeTO())){
					q.setParameter("productTypeTO", searchTO.getProductTypeTO());
				}

				if (searchTO.getSeasonTO()!=null && !"".equals(searchTO.getSeasonTO())){
					q.setParameter("seasonTO", searchTO.getSeasonTO());
				}

				if (searchTO.getSexTO()!=null && !"".equals(searchTO.getSexTO())){
					q.setParameter("sexTO", searchTO.getSexTO());
				}

				if (searchTO.getAssistCode()!=null && !"".equals(searchTO.getAssistCode())){
					q.setParameter("assistCode", searchTO.getAssistCode());
				}

				return q.list();
			}

		});
		return executeFind;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ProductTO getProductTOByProductId(String productId) {
		List l = getHibernateTemplate().find("from com.balance.anole.business.to.ProductTO as p where p.pdtId = ?",productId);
		if(l != null && l.size() > 0){
			return (ProductTO)l.get(0);
		}
		return null;
	}
}
